var log = require("./../../util/loginfo").getInstand;
Fish = function (_userInfo) {
    this._userId = 0;
    this._playCount = Math.floor(Math.random() * 2) + 1;
    this._online = false;
    this._hallSocket = null;
    this._gameSocket = null;
    this._account = null;
    this._sign = null;
    this._socre = 0;
    this._idx = 0;
    this._gameInfo = {};
    this._checkPai = false;
    this._userList = new Set();

    this.init = () => {
        this._idx = _userInfo.idx;
        this._account = _userInfo.account;
        //连接大厅
//        this._hallSocket = io.connect('http://192.168.1.110:13000');
        this._hallSocket = io.connect('http://localhost:13000');
        this.addHallSocketListeners();
    }

    this.addHallSocketListeners = () => {
        this._hallSocket.on('connected', () => {
            this._hallSocket.emit('login', {
                userName: this._account,
                sign: '@@@@youmegame2019`@112233445566@@@@',
                gameId: 1
            })
        });

        this._hallSocket.on('loginResult', res => {
            // log.info('^^^loginResult:'+JSON.stringify(res));
            this._sign = res.Obj.sign;
            this._userId = res.Obj.id;
            this._socre = res.Obj.score;
            this._online = true;
        });

        this._hallSocket.on('ServerListResult', res => {
            //连接游戏server
            // log.info('^^^ServerListResult:'+ JSON.stringify(res));
            let index = 0;
            for (let i = 0; i < res.GameInfo.length; i++) {
                if (res.GameInfo[i].GameId == 1) {
                    index = i;
                    break;
                }
            }
            this._gameSocket = io.connect('http://' + res.GameInfo[index].serverInfo.normal[this._idx].ip + ':' + res.GameInfo[index].serverInfo.normal[this._idx].prot);
            this.addFishSocketListeners();
        })
    };

    this.addFishSocketListeners = () => {
        this._gameSocket.on('connected', () => {
            this._gameSocket.emit("LoginGame", {userid: this._userId, gametype: 11, sign: this._sign});
        });

        this._gameSocket.on('loginGameResult', res => {
            log.info('^^^^进入捕鱼游戏：' + JSON.stringify(res));
            if (res.resultid) {
                this._hallSocket.disconnect();
                this._gameInfo = {};
                this._gameInfo.playerCoin = res.Obj.score;
                this._gameInfo.bet = res.Obj.bet;
                this._gameSocket.emit('LoginRoom', {roomid: 1});
            }
        });

        this._gameSocket.on('LoginRoomResult', res => {
            //log.info('^^^^进入捕鱼房间：'+JSON.stringify(res));
            if (res.ResultCode) {
                this._gameInfo.tableId = res.ResultData.TableId;
                this._gameInfo.seatIndex = res.ResultData.seatId;
                this._gameInfo.tax = res.ResultData.tax;
                this._gameInfo.addScore = res.ResultData.addscore;

                this._gameInfo.playerCount = res.ResultData.userList.length;

                this.ready();
                this.shoot();
            }
        });

        //用户进入
        this._gameSocket.on('playEnter', res => {
            this._gameInfo.playerCount++;
        });

        //用户退出
        this._gameSocket.on('PlayerOut', res => {
            this._gameInfo.playerCount--;
        });

        this._gameSocket.on('login_result', res => {
            for (let i in res.seats) {
                if (res.seats[i].userid != 0) {
                    this._userList.add(res.seats[i].userid);
                }
            }
        })

        this._gameSocket.on('fishHit_boardResult', res => {
            if (res.uid == this._userId) {
                res.isRobot = true;
                this._gameSocket.emit('fishHit', res);
            }
        })


    }
    this.bulletId = 0;
    this.shootChangeCount = 0;
    this.shootPos = {x: 0, y: 0};
    this.shoot = () => {
        if (!this._gameSocket) return;

        if (this._gameInfo.playerCoin <= this._gameInfo.bet * 2) {
            this._gameSocket.disconnect();
            return;
        }


        if (this._gameInfo.playerCount > 1) {
            if (this.shootChangeCount == 0) {
                this.shootChangeCount = parseInt(Math.random() * 10) + 8;
                this.shootPos.x = parseInt(Math.random() * 700) + 300;
                this.shootPos.y = parseInt(Math.random() * 550) + 100;
            } else {
                this.shootChangeCount--;
            }

            this._gameSocket.emit("LoginGame", {userid: this._userId, gametype: 11, sign: this._sign});
            //log.info("this.shootChangeCount "+this.shootChangeCount);
            if (this.shootChangeCount >= 3) {
                this._gameSocket.emit('fishShoot', JSON.stringify({
                    userid: this._userId,
                    bet: 1,
                    position: this.shootPos,
                    bulletId: this.getNextBulletID(),
                    isRobot: true,
                }));
                this._gameInfo.playerCoin -= this._gameInfo.bet;
            }
        }


        setTimeout(() => {
            this.shoot();
        }, 200);
    }

    this.getNextBulletID = () => {
        if (this.bulletId >= 1e4)
            this.bulletId = 0;
        else
            this.bulletId++;
        //log.info("send: "+this.bulletId);
        return this.bulletId;
    },

        this.ready = () => {
            this._checkPai = false;
            this._gameSocket.emit('ready');
        }

    this.isOnline = () => {
        return this._playCount > 0;
    }

    this.disconnect = () => {
        if (this._hallSocket) {
            this._hallSocket.disconnect();
            this._hallSocket = null;
        }
        if (this._gameSocket) {
            this._gameSocket.disconnect();
            this._gameSocket = null;
        }
        this._online = false;
    }

    this.init();
}
